package com.eva.admin.api.cms;

import com.eva.admin.biz.cms.category.dto.UpdateCmsCategoryStatusDTO;
import com.eva.admin.biz.cms.category.vo.CmsCategoryNodeVO;
import com.eva.framework.cache.clean.CleanCache;
import com.eva.framework.common.api.BaseController;
import com.eva.admin.biz.cms.category.CmsCategoryBiz;
import com.eva.admin.biz.cms.category.dto.UpdateCmsCategoryDTO;
import com.eva.admin.biz.cms.category.dto.CreateCmsCategoryDTO;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.security.prevent.PreventRepeat;
import com.eva.framework.common.model.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "栏目管理")
@RestController
@RequestMapping("/cms/category")
public class CmsCategoryController extends BaseController {

    @Resource
    private CmsCategoryBiz cmsCategoryBiz;

    @PreventRepeat
    @CleanCache(keys = {"CATEGORIES_TREE", "category:*:children"})
    @ApiOperation("新建")
    @PostMapping("/create")
    @ContainPermissions("cms:category:create")
    public ApiResponse<?> create(@RequestBody CreateCmsCategoryDTO dto) {
        cmsCategoryBiz.create(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("删除")
    @CleanCache(keys = {"CATEGORIES_TREE", "category:*:children"})
    @GetMapping("/delete/{id}")
    @ContainPermissions("cms:category:delete")
    public ApiResponse<?> deleteById(@PathVariable Integer id) {
        cmsCategoryBiz.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @CleanCache(keys = {"CATEGORIES_TREE", "category:*:children"})
    @GetMapping("/delete/batch")
    @ContainPermissions("cms:category:delete")
    public ApiResponse<?> deleteByIdInBatch(@RequestParam String ids) {
        cmsCategoryBiz.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("修改")
    @CleanCache(keys = {"CATEGORIES_TREE", "category:*:children"})
    @PostMapping("/updateById")
    @ContainPermissions("cms:category:update")
    public ApiResponse<?> updateById(@RequestBody UpdateCmsCategoryDTO dto) {
        cmsCategoryBiz.updateById(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("修改状态")
    @CleanCache(keys = {"CATEGORIES_TREE", "category:*:children"})
    @PostMapping("/updateStatus")
    @ContainPermissions("cms:category:update")
    public ApiResponse<?> updateStatus(@RequestBody UpdateCmsCategoryStatusDTO dto) {
        cmsCategoryBiz.updateStatus(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("查询树")
    @GetMapping("/tree")
    @ContainPermissions("cms:category:query")
    public ApiResponse<List<CmsCategoryNodeVO>> findTree () {
        return ApiResponse.success(cmsCategoryBiz.findTree());
    }
}
